import 'package:flutter/material.dart';

import 'route_delegate_decoration.dart';

///路由控制
class NavigatorController {
  static const ROOT = "/";

  NavigatorController._();

//页面销毁
  static void finishPage<T extends Object>(BuildContext context, [T? result]) {
    RouteDelegateDecoration.of(context).pop(result);
  }

//页面跳转
  static Future<T> startPage<T extends Object>(
    BuildContext context,
    Widget page, {
    bool isReplaceCurrent = false,
    bool isRoot = false,
  }) {
    assert(!(isRoot == true && isReplaceCurrent == true));
    RouteDelegateDecoration delegate = RouteDelegateDecoration.of(context);
    if (isReplaceCurrent == true) {
      return delegate.pushToReplace(page);
    } else if (isRoot) {
      return delegate.pushToRoot(page);
    } else {
      return delegate.push(page);
    }
  }

//显示dialog
  static Future<T> showDialog<T extends Object>(
    BuildContext context,
    Widget page,
  ) {
    return RouteDelegateDecoration.of(context).push(page, isDialog: true);
  }

  //不显示dialog
  static void hideDialog<T extends Object>(BuildContext context, Widget page,
      [T? result]) {
    RouteDelegateDecoration.of(context).remove(page: page, isdialog: true);
  }
}
